#ifndef SEQIOT_IEC103_H
#define SEQIOT_IEC103_H

#include <complex.h>
#include "../iec60870/iec101.h"

typedef enum _type_id_103_e {
    M_TM_TA_3 = 1,
    M_TMR_TA_3 = 2,
    M_ME1_NA_3 = 3,
    M_TME_TA_3 = 4,
    M_IRC_NA_3 = 5,
    M_SYN_TA_3 = 6,
    M_TGI_NA_3 = 8,
    M_ME2_NA_3 = 9,
    M_GD_NTA_3 = 10,
    M_GI_NTA_3 = 11,
    M_LRT_TA_3 = 23,
    M_RTD_TA_3 = 26,
    M_RTC_NA_3 = 27,
    M_RTT_NA_3 = 28,
    M_TDT_TA_3 = 29,
    M_TDN_NA_3 = 30,
    M_EOT_NA_3 = 31,
    M_ME3_TA_3 = 32,
    M_ME4_TA_3 = 33,
    M_ME5_TA_3 = 34,
    M_ME6_TA_3 = 35,
    M_ME7_NA_3 = 50,
    M_IT_NA_3 = 36,
    M_IT_TA_3 = 37,
    M_ST_NA_3 = 38,
    M_ST_TA_3 = 39,
    M_SP_NA_3 = 40,
    M_SP_TA_3 = 41,
    M_DP_NA_3 = 42,
    M_DP_TA_3 = 43,
    M_SS_NA_3 = 44,
    M_SS_TA_3 = 45,
    M_DS_NA_3 = 46,
    M_DS_TA_3 = 47,
    M_WL_TA_3 = 48,

    M_DC_NA_3 = 64,
    M_RC_NA_3 = 65,
    M_SE_NA_3 = 66,
    M_CC_NA_3 = 67,
    M_CI_NA_3 = 88,

    C_SYN_TA_3 = 6,
    C_IGI_NA_3 = 7,
    C_GD_NA_3 = 10,
    C_GRC_NA_3 = 20,
    C_GC_NA_3 = 21,
    C_ODT_NA_3 = 24,
    C_ADT_NA_3 = 25,
    C_DC_NA_3 = 64,
    C_RC_NA_3 = 65,
    C_SE_NA_3 = 66,
    C_CC_NA_3 = 67,
    C_CI_NA_3 = 88
} type_id_103_e;

typedef enum _function_type_t {
    fun_BU1 = 1,
    fun_BU2 = 2,
    fun_BU3 = 3,
    fun_BU4 = 4,
    fun_BU5 = 5,
    fun_BU6 = 6,

    fun_dis_prt = 128,
    fun_dis_prt1 = 130,

    fun_cur_prt = 160,
    fun_cur_prt1 = 133,
    fun_cur_prt2 = 136,
    fun_cur_prt3 = 139,
    fun_cur_prt4 = 142,

    fun_tra_prt = 176,
    fun_tra_prt1 = 146,

    fun_lin_prt = 192,
    fun_lin_prt1 = 152,

    fun_LP1 = 178,
    fun_AP1 = 184,
    fun_TP1 = 194,
    fun_TP2 = 198,
    fun_BS1 = 210,
    fun_GR1 = 226,
    fun_GR2 = 230,
    fun_LP2 = 242,
    fun_AP2 = 248,

    fun_GEN = 254,
    fun_GLB = 255,
} func_type_t;

typedef enum _gen_fun_inf_t {
    read_title = 240,
    read_group_attrVal = 241,
    read_entry_dir = 243,
    read_entry_attrVal = 244,
    generic_query = 245,
    write_entry = 248,
    write_entry_afm = 249,
    write_entry_exec = 250,
    write_entry_cancel = 251,
    GEN_FUN = 254,
} gen_fun_inf_t;

struct _fun_inf_t {
    uint8_t fun;
    uint8_t inf;
}__attribute__((packed)) fun_inf_t;

typedef enum {
    global, ia, ib, ic, in, ua, ub, uc, un
} ACC;//7.2.6.1
typedef char ASC;//7.2.6.2
typedef enum {
    BU = 2, GEN = 3
} COL;//7.2.6.3
typedef dpi_e DCO;//7.2.6.4
typedef dpi_e DPI;//7.2.6.5
typedef uint16_t FAN;//7.2.6.6
typedef uint16_t INT;//7.2.6.7
typedef struct {
    _Bool ov: 1;
    _Bool er: 1;
    _Bool res: 1;
    int mval: 13;
}__attribute__((packed)) MEA;//7.2.6.8
typedef uint16_t NFE;//7.2.6.9
typedef uint8_t NOC;//7.2.6.10
typedef uint16_t NOE;//7.2.6.11
typedef uint16_t NOF;//7.2.6.12
typedef uint8_t NOT;//7.2.6.13
typedef uint8_t NDV;//7.2.6.14
typedef uint16_t RET;//7.2.6.15
typedef float RFA;//7.2.6.16
typedef float RPV;//7.2.6.17
typedef float RSV;//7.2.6.18
typedef uint8_t RII;//7.2.6.19
typedef float SCL;//7.2.6.20
typedef uint8_t SCN;//7.2.6.21
typedef int16_t SDV;//7.2.6.22
typedef uint8_t SIN;//7.2.6.23
typedef struct {
    _Bool tp: 1;
    _Bool tm: 1;
    _Bool test: 1;
    _Bool otev: 1;
}__attribute__((packed)) SOF;//7.2.6.24
typedef uint16_t TAP;//7.2.6.25
typedef uint8_t TOO;//7.2.6.26
typedef enum {
    unused, transient
} TOV;//7.2.6.27
typedef struct {
    uint8_t no: 6;
    _Bool count: 1;
    _Bool cont: 1;
}__attribute__((packed)) NGD;//7.2.6.30
typedef struct {
    uint8_t group;
    uint8_t entry;
}__attribute__((packed)) GIN;//7.2.6.31
typedef enum {
    gdd_none = 0,
    gdd_OS8,
    gdd_BS,
    gdd_UINT,
    gdd_INT,
    gdd_UFLOAT,
    gdd_FLOAT,
    gdd_R32,
    gdd_R64,
    gdd_DPI,
    gdd_SPI,
    gdd_DPI_ERR,
    gdd_MEA,
    gdd_res0,
    gdd_cp56time2a,
    gdd_GIN,
    gdd_RET,
    gdd_FUN_INF,
    gdd_DPI48,
    gdd_DPI80,
    gdd_MEA96,
    gdd_UIi,
    gdd_GRC,
    gdd_STRUCT,
    gdd_INDEX,
} GDD_data_type_e;
typedef struct {
    GDD_data_type_e data_type;
    uint8_t data_size;
    uint8_t number: 7;
    _Bool cont: 1;
}__attribute__((packed)) GDD;//7.2.6.32
typedef enum {
    kod_no_spec = 0,
    kod_actual = 1,
    kod_default = 2,
    kod_range = 3,
    kod_percision = 5,
    kod_factor = 6,
    kod_reference = 7,
    kod_enumeration = 8,
    kod_dimension = 9,
    kod_description = 10,
    kod_password = 12,
    kod_read_only = 13,
    kod_write_only = 14,
    kod_corr_fun_inf = 19,
    kod_corr_events = 20,
    kod_text_arr = 21,
    kod_val_arr = 22,
    kod_related = 23
} KOD;//7.2.6.34
typedef NGD NDE;//7.2.6.35
typedef enum {
    grc_afm = 0,
    grc_inv_gin,
    grc_no_data,
    grc_re_send,
    grc_err_chg,
    grc_inv_chg,
    grc_rag_ovr,
    grc_more_op,
    grc_rd_only,
    grc_password,
    grc_local,
    grc_err,
} GRC;//7.2.6.36
typedef uint8_t NOG;//7.2.6.37

typedef struct {
    MEA r, i;
}__attribute__((packed)) MEA_i;//E2.13
typedef struct {
    uint8_t sq: 4;
    uint8_t hrtm: 4;
    _Bool ov: 1;
    _Bool er: 1;
    uint8_t res: 3;
    int mval: 11;
}__attribute__((packed)) MEAHR;//E2.14
typedef struct {
    uint8_t sq: 3;
    uint8_t hrtm: 4;
    _Bool er: 1;
    int mval_r: 11;
    _Bool ov_r: 1;
    int mval_i: 11;
    _Bool ov_i: 1;
}__attribute__((packed)) MEAHR_i;//E2.15

typedef struct {
    DPI dpi;
    cp32time2a_t tm;
    SIN sin;
}__attribute__((packed)) ASDU_1;

typedef struct {
    DPI dpi;
    RET ret;
    FAN fan;
    cp32time2a_t tm;
    SIN sin;
}__attribute__((packed)) ASDU_2;

typedef struct {
    SCL scl;
    RET ret;
    FAN fan;
    cp32time2a_t tm;
}__attribute__((packed)) ASDU_4;

typedef struct {
    COL col;
    char vendor[8];
    char model[4];
}__attribute__((packed)) ASDU_5;

typedef struct {
    cp56time2a_t dataTime;
}__attribute__((packed)) ASDU_6;

typedef struct {
    SCN scn;
}__attribute__((packed)) ASDU_7;

typedef struct {
    SCN scn;
}__attribute__((packed)) ASDU_8;

typedef struct {
    RII rii;
    NGD ngd;
}__attribute__((packed)) ASDU_10;

typedef struct {
    GIN gin;
    KOD kod;
    GDD gdd;
    unsigned char gid[64];
}__attribute__((packed)) ASDU_10_item;

typedef struct {
    MEA ia, ib, ic, ua, ub, uc, p, q, f;
    MEA_i hr[16];
    cp32time2a_t *tm;
}__attribute__((packed)) ASDU_33;

#endif //SEQIOT_IEC103_H
